<template>
  <el-container style="padding: 15px">
    <el-header height="120px">
      <el-form size="small" label-width="70px">
        <el-row type="flex" :gutter="20" align="middle">
          <el-col :span="6">
            <el-form-item label="项目编号">
              <el-input v-model="searchObj.reportId" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="项目名称">
              <el-input v-model="searchObj.reportName" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="项目类型">
              <el-select v-model="searchObj.reportType" style="width: 100%" clearable filterable>
                <el-option v-for="item in projectType" :key="item.id" :value="item.id" :label="item.reportType" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="项目成果">
              <el-select v-model="searchObj.achievement" style="width: 100%" clearable filterable>
                <el-option v-for="item in achievement" :key="item.id" :value="item.id" :label="item.achievementName" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" align="middle" :gutter="20">
          <el-col :span="6">
            <el-form-item label="审核阶段">
              <el-select v-model="searchObj.leafId" style="width: 100%" clearable filterable>
                <el-option v-for="item in leaf" :key="item.id" :value="item.id" :label="item.leafName" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="业务人员">
              <el-select v-model="searchObj.salesman" style="width: 100%" clearable filterable>
                <el-option v-for="item in users" :key="item.id" :value="item.id" :label="item.nickName" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="评估日期">
              <el-date-picker
                v-model="searchObj.startTime"
                value-format="timestamp"
                type="date"
                placeholder="开始时间"
                @change="startTimeChangeHandler"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label=" ">
              <el-date-picker
                v-model="searchObj.endTime"
                value-format="timestamp"
                type="date"
                placeholder="结束时间"
                @change="EndTimeChangeHandler"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" align="center" justify="center">
          <el-col :span="4">
            <el-button type="primary" size="small" @click="getProjectList">搜索</el-button>
            <el-button size="small" @click="clearSearch">重置</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-header>
    <el-divider />
    <el-main>
      <el-form size="small">
        <el-row type="flex">
          <el-col :span="4">
            <el-form-item>
              <el-button type="primary" :loading="downloadingButtonLoding" @click="download">下载</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-table :data="projectList">
        <el-table-column label="项目编号" prop="reportId" width="200px" />
        <el-table-column label="项目名称" prop="reportName" width="200px" :formatter="subName" />
        <el-table-column label="项目类型" prop="reportType" width="200px" />
        <el-table-column label="开始时间" prop="reportDateStart" width="200px" :formatter="tableTimeFormat" />
        <el-table-column label="结束时间" prop="reportDateEnd" width="200px" :formatter="tableTimeFormat" />
        <el-table-column label="项目成果" prop="achievement" width="200px" />
        <el-table-column label="归属部门" prop="affiliation" width="200px" />
        <el-table-column label="项目负责人" prop="principal" width="200px" />
        <el-table-column label="项目组成员1" prop="member1" width="200px" />
        <el-table-column label="项目占比1" prop="itemProportion1" width="200px" />
        <el-table-column label="项目组成员2" prop="member2" width="200px" />
        <el-table-column label="项目占比2" prop="itemProportion2" width="200px" />
        <el-table-column label="项目组成员3" prop="member3" width="200px" />
        <el-table-column label="项目占比3" prop="itemProportion3" width="200px" />
        <el-table-column label="项目收费" prop="charge" width="200px" />
        <el-table-column label="业务人员" prop="salesman" width="200px" />
        <el-table-column label="部门" prop="department" width="200px" />
        <el-table-column label="组别" prop="tranches" width="200px" />
        <el-table-column label="用印类别" prop="stampCategory" width="200px" />
        <el-table-column label="用印份数" prop="stampNumber" width="200px" />
        <el-table-column label="资料是否齐全" prop="administrativeCompleteMaterial" width="200px" />
        <el-table-column label="是否归档" prop="administrativeFile" width="200px" />
        <el-table-column label="存档时间" prop="administrativeFileDate" width="200px" :formatter="tableTimeFormat" />
        <el-table-column label="是否作废" prop="administrativeCancellation" width="200px" />
        <el-table-column label="未存档原因" prop="administrativeCause" width="200px" />
        <el-table-column label="行政-备注" prop="administrativeRemark" width="200px" />
        <el-table-column label="第一次收费" prop="financeOneCharge" width="200px" />
        <el-table-column label="第二次收费" prop="financeTwoCharge" width="200px" />
        <el-table-column label="第三次收费" prop="financeThreeCharge" width="200px" />
        <el-table-column label="第一次到账时间" prop="financeOnePayTime" width="200px" :formatter="tableTimeFormat" />
        <el-table-column label="第二次到账时间" prop="financeTwoPayTime" width="200px" :formatter="tableTimeFormat" />
        <el-table-column label="第三次到账时间" prop="financeThreePayTime" width="200px" :formatter="tableTimeFormat" />
        <el-table-column label="到账金额到账金额" prop="financeArrivalAmount" width="200px" />
        <el-table-column label="到账时间" prop="financePayDate" width="200px" :formatter="tableTimeFormat" />
        <el-table-column label="开票金额" prop="financeBillCharge" width="200px" />
        <el-table-column label="票据情况" prop="financeBillStatus" width="200px" />
        <el-table-column label="开票时间" prop="financeBillDate" width="200px" :formatter="tableTimeFormat" />
        <el-table-column label="开票信息备注" prop="financeTicketInformation" width="200px" />
        <el-table-column label="财务备注" prop="financeRemark" width="200px" />
        <el-table-column label="管理员操作记录" prop="record" width="200px" />
      </el-table>
      <el-pagination
        :current-page="currentPage"
        :page-sizes="[10, 20, 40, 80]"
        :page-size="sizePage"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalPage"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-main>
  </el-container>
</template>

<script>
import { searchStatisticsProject, getSearchFormList, downloadFinance } from '@/api/project-report'
import { ellipsisString, parseTime } from '@/utils'
import { validObj } from '@/utils/validate'

export default {
  name: 'Index',
  data() {
    return {
      searchObj: {},
      projectType: [],
      achievement: [],
      leaf: [],
      users: [],
      projectList: [],
      currentPage: 1,
      sizePage: 10,
      totalPage: 0,
      downloadingButtonLoding: false
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      this.getProjectList()
      this.getSearchForm()
    },

    getProjectList() {
      const currentPage = this.currentPage
      const sizePage = this.sizePage
      const searchObj = this.searchObj
      searchStatisticsProject(searchObj, currentPage, sizePage).then(res => {
        this.projectList = res.data.list
        this.totalPage = res.data.total
      })
    },
    clearSearch() {
      this.searchObj = {}
      this.getProjectList()
      // 重置成功提示框
      this.$message({
        message: '重置成功',
        type: 'success'
      })
    },
    subName(row, column, cellValue, index) {
      return ellipsisString(cellValue)
    },
    getSearchForm() {
      getSearchFormList().then(res => {
        this.projectType = res.data.projectType
        this.achievement = res.data.achievement
        this.leaf = res.data.leaf
        this.users = res.data.users
      })
    },
    download() {
      const searchObj = this.searchObj
      if (validObj(searchObj)) {
        this.downloadingButtonLoding = true
        downloadFinance(searchObj).then(res => {
          const url = window.URL.createObjectURL(new Blob([res]))
          const link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.setAttribute('download', '项目统计表单.xlsx')
          document.body.appendChild(link)
          link.click()
          this.downloadingButtonLoding = false
        }).finally(() => {
          this.downloadingButtonLoding = false
        })
      } else {
        this.$message.warning('搜索信息不能为空')
      }
    },
    handleSizeChange(val) {
      this.sizePage = val
      this.getProjectList()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getProjectList()
    },
    tableTimeFormat(row, column, cellValue, index) {
      return parseTime(cellValue, '')
    }
  }
}
</script>

<style scoped>

</style>
